<template>
  <div>
    <div class="options">
      <el-button @click="info.isShow=true" type="primary" plain round>新增</el-button>
    </div>
    <el-table :data="goodsList" style="width: 100%">
      <el-table-column prop="id" label="编号" min-width="60" fixed="left">
      </el-table-column>
      <el-table-column prop="firstcatename" label="一级分类" min-width="180">
      </el-table-column>
      <el-table-column prop="secondcatename" label="二级分类" min-width="180">
      </el-table-column>
      <el-table-column prop="goodsname" label="商品名称" min-width="180">
      </el-table-column>
      <el-table-column prop="img" label="商品图片" min-width="180">
        <template v-slot="props">
          <img :src="$baseUrl + props.row.img" style="height: 120px" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="price" label="商品价格" min-width="180">
        <template v-slot="props">
          <b style="color: red">&yen; {{ props.row.price }}</b>
        </template>
      </el-table-column>
      <el-table-column prop="market_price" label="市场价格" min-width="180">
        <template v-slot="props">
          <span style="color: red">&yen; {{ props.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="isnew" label="是否新品" min-width="180">
        <template v-slot="props">
          <el-tooltip
            :content="props.row.isnew === 1 ? '是' : '否'"
            placement="top"
          >
            <el-switch
              v-model="props.row.isnew"
              :active-value="1"
              :inactive-value="0"
              @change="goodsStateUpdate(props.row)"
            >
            </el-switch>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="ishot" label="是否热卖" min-width="180">
        <template v-slot="props">
          <el-tooltip
            :content="props.row.ishot === 1 ? '是' : '否'"
            placement="top"
          >
            <el-switch
              v-model="props.row.ishot"
              :active-value="1"
              :inactive-value="0"
              @change="goodsStateUpdate(props.row)"
            >
            </el-switch>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" min-width="180">
        <template v-slot="props">
          <el-button
            v-if="props.row.status === 1"
            type="primary"
            size="mini"
            plain
            round
            >正常</el-button
          >
          <el-button v-else type="warning" size="mini" plain round
            >禁用</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="160" fixed="right">
        <template v-slot="props">
          <el-button type="primary" @click="edit(props.row)" size="mini" plain round>编辑</el-button>
        <el-button @click="del(props.row.id)" type="warning" size="mini" plain round>删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
     :page-sizes="[5, 10, 15, 20]" 
     @size-change="sizeUpdate"  
     @current-change="pageUpdate" :page-size="size" background layout="prev, pager, next,sizes" :total="goodsTotal">
    </el-pagination>
    <!-- 调用对话框组件 -->
    <Dialog ref="dialog" :info="info" @goodsUpdate="goodsUpdate"/>
  </div>
</template>

<script>
// 导入对话框组件
import Dialog from './Dialog';
// 导入vuex辅助函数
import { mapState, mapActions } from "vuex";
export default {
  components:{Dialog},
  data() {
    return {
      info:{
        // 控制对话框组件显示状态
        isShow:false
      },
      // 页码
      page: 1,
      // 每页显示数据条数
      size: 5,
    };
  },
  computed: {
    ...mapState("goods", ["goodsList", "goodsTotal"]),
  },
  methods: {
    ...mapActions("goods", ["getGoodsListAction", "getGoodsTotalAction"]),
    // 商品删除
    del(id){
      this.$myConfirm(()=>{
        this.$axios.post('/goodsdelete',{id}).then(res=>{
          if(res.code===200){
            this.$message.success(res.msg);
            // 更新数据
            this.goodsUpdate();
          }else{
            this.$message.error(res.msg);
          }
        })
      })
    },
    // 编辑按钮点击事件处理函数
    edit(row){
      // 1-显示对话框组件
      this.info.isShow=true;
      // 2-将待编辑的商品对象传递给子组件做默认展示
      this.$refs.dialog.setFormData(row);
    },
    // 自定义事件goodsUpdate的处理函数
    goodsUpdate(){
        this.getGoodsListAction({ page: this.page, size: this.size });
        this.getGoodsTotalAction();
    },
    // switch开关组件change事件处理函数
    goodsStateUpdate(obj) {
      // 创建FormData实例对象
      const fd = new FormData();
      fd.append("id", obj.id);
      fd.append("isnew", obj.isnew);
      fd.append("ishot", obj.ishot);
      this.$axios.post("/goodsedit", fd).then((res) => {
        if (res.code === 200) {
          this.$message.success("操作成功");
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    // 分页按钮点击事件处理函数
    pageUpdate(page){
        // 1-更新page
        this.page=page;
        // 2-根据最新的page, 请求分页数据
        this.getGoodsListAction({ page: this.page, size: this.size });
    },
    // 监听size的变化
    sizeUpdate(size){
        // 1-更新每页显示的数据条数size
        this.size=size;
        // 2-根据最新的size请求列表数据
        this.getGoodsListAction({ page: this.page, size: this.size });
    }
  },
  created() {
    // 请求商品列表
    this.getGoodsListAction({ page: this.page, size: this.size });
    // 请求商品总数
    this.getGoodsTotalAction();
  },
};
</script>

<style>
</style>